package eyrolles;

import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ActionMessage;

public class AddEmployeAction extends ActionDB {

    protected void insertUser(ActionForm form)
            throws SQLException {

        String user = null;

        try {

            EmployeForm eForm = (EmployeForm) form;
            conn = getConnection();
            stmt = conn.createStatement();

            StringBuffer sqlString =
                    new StringBuffer("insert into employes ");

            sqlString.append("values ('"
                    + eForm.getUsername() + "', ");
            sqlString.append("'"
                    + eForm.getPassword() + "', ");
            sqlString.append("'"
                    + eForm.getName() + "', ");
            sqlString.append(
                    eForm.getRoleid() + ", ");
            sqlString.append("'"
                    + eForm.getPhone() + "', ");
            sqlString.append("'"
                    + eForm.getEmail() + "', ");
            sqlString.append(
                    eForm.getDepid() + ")");

            stmt.execute(sqlString.toString());
        }catch (NullPointerException e) {

            throw new SQLException();
        } finally {

            closeConnection();
        }
    }

    public ActionForward execute(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
            throws IOException, ServletException {

        ActionMessages errors = new ActionMessages();

        // Cible par defaut en cas de succes
        String target = new String("success");

        // teste l'identification de l'utilisateur    
        HttpSession session = request.getSession();
        if (session.getAttribute("USER") == null) {

            // L'utilisateur n'est pas identifie
            target = new String("login");

            errors.add(ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("errors.login.required"));
            
            // Signalement des erreurs eventuelles
            if (!errors.isEmpty()) {

                saveErrors(request, errors);
            }
            
            // Si jamais l'utilisateur n'est pas loggé, alors
            // on retourne directement à la page de login
            // avant d'effectuer la moindre requête    
            return (mapping.findForward(target));
        }

        if (isCancelled(request)) {

            // Annulation. Retour a la liste des employes
            return (mapping.findForward(target));
        }

        try {

            insertUser(form);
        } catch (SQLException e) {

            target = new String("error");
            errors.add(ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("errors.database.error"));            
        }
        
        // Signalement des erreurs eventuelles
            if (!errors.isEmpty()) {

                saveErrors(request, errors);
            }
            
        // Transmission a la vue appropriee
        return (mapping.findForward(target));
    }
}